package com.hdaccp.personal.Controller;

import com.hdaccp.personal.dao.ShoucangDao;
import com.hdaccp.personal.service.ShoucangSerivce;
import com.hdaccp.pojo.po.shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/shoucang")
public class ShoucangController  {


    @Autowired
    private ShoucangDao shoucangDao;
    @Autowired
    private ShoucangSerivce shoucangSerivce;



    @RequestMapping("/query")
    public List<shop> query(){
        return shoucangDao.findAll();
    }

    @RequestMapping("/getshopId/{shoucangId}")
    public shop getshopid(@PathVariable("shoucangId") int shoucangId){
        return shoucangDao.findById(shoucangId).get();
    }

    @RequestMapping("/queryPage/{cpage}/{pagesize}")
    public List<shop> queryPage(@PathVariable("cpage") int cppage,@PathVariable("pagesize") int pagesize){
        return shoucangSerivce.querypage(cppage,pagesize);
    }

    @RequestMapping("/save/{shopname}/{shopmoney}")
    public String save(@PathVariable("shopname") String shopname,@PathVariable("shopmoney") int shopmoney){
        String msg="success";
        shop s=new shop();
        s.setShopname(shopname);
        s.setShopmoney(shopmoney);
        try {
            shoucangDao.save(s);
        } catch (Exception e){
            msg = "shibai";
        }
        return msg;
    }


    @RequestMapping("/update/{shopId}/{shopname}/{shopmoney}")
    public String update(@PathVariable("shopId") int shopid,@PathVariable("shopname") String shopname,@PathVariable("shopmoney") int shopmoney){
        String msg="success";
        shop s=shoucangDao.findById(shopid).get();
        s.setShopname(shopname);
        s.setShopmoney(shopmoney);
        try{
            shoucangDao.save(s);
        }catch (Exception e){
            msg="shibai";
        }
        return msg;
    }



    @RequestMapping("/del/{shopId}")
    public String delete(@PathVariable("shopId") int shopid){
        String msg="success";
        try{
            shoucangDao.deleteById(shopid);
        }catch (Exception e){
            msg="shibai";
        }
        return msg;
    }
}